import time

from flask import g, Flask, render_template, session, redirect, request
from flask_login import LoginManager, current_user
from flask_migrate import Migrate

from exts import db
from models import User
import flask_configs

import redis
from configs import REDIS_PORT, REDIS_HOST, REDIS_DB

from blueprints.user import bp as user_bp
from blueprints.visitor import bp as visitor_bp
from blueprints.api.user import bp as api_user_bp
from blueprints.api.capture import bp as api_capture_bp
from blueprints.api.passers_by import bp as api_passers_by_bp
from blueprints.passers_by import bp as passers_by_bp

app = Flask(__name__)
# app.app_context().push()
app.config.from_object(flask_configs)
db.init_app(app)

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "visitor.login"
login_manager.login_message_category = "warning"

app.register_blueprint(user_bp)
app.register_blueprint(visitor_bp)
app.register_blueprint(api_user_bp)
app.register_blueprint(api_capture_bp)
app.register_blueprint(api_passers_by_bp)
app.register_blueprint(passers_by_bp)

migrate = Migrate(app, db)


@login_manager.user_loader
def load_user(user_id):
    user = User.query.get(int(user_id))
    return user


@app.before_request
def before_request():
    g.my_redis = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB)
    if current_user.is_authenticated:
        g.user = current_user


# @app.route("/test01")
# def test01():
#     time.sleep(5)
#
#     return "test01"
#
#
# @app.route("/test02")
# def test02():
#
#     return "test02"


if __name__ == '__main__':
    app.run()
